package com.wxzhou.wangzhuan.dto;

import org.eclipse.microprofile.openapi.annotations.media.Schema;

import java.io.Serializable;

/**
 * 通用响应对象
 * @param <T> 业务数据类型
 */
@Schema(description = "通用响应对象", example = "{\"code\":\"200\",\"msg\":\"成功\",\"data\":{}}")
public class ResponseDto<T> implements Serializable {
    /** 业务数据 */
    @Schema(description = "业务数据", example = "{}")
    private T data;
    /** 提示信息 */
    @Schema(description = "提示信息", example = "成功")
    private String msg;
    /** 状态码 */
    @Schema(description = "状态码：200(成功), 400(失败), 401(未授权), 403(禁止), 404(未找到), 500(错误)", example = "200")
    private int code;



    public ResponseDto() {}
    public ResponseDto(T data, String msg, int code) {
        this.data = data;
        this.msg = msg;
        this.code = code;
    }



    /**
     * 成功响应（带数据）
     */
    public static <T> ResponseDto<T> success(T data) {
        return new ResponseDto<>(data, "成功", 200);
    }

    /**
     * 成功响应（无数据）
     */
    public static <T> ResponseDto<T> success() {
        return new ResponseDto<>(null, "成功", 200);
    }

    /**
     * 成功响应（自定义消息）
     */
    public static <T> ResponseDto<T> success(T data, String msg) {
        return new ResponseDto<>(data, msg, 200);
    }

    /**
     * 失败响应
     */
    public static <T> ResponseDto<T> fail(String msg) {
        return new ResponseDto<>(null, msg, 400);
    }

    /**
     * 失败响应（自定义状态码）
     */
    public static <T> ResponseDto<T> fail(String msg, int code) {
        return new ResponseDto<>(null, msg, code);
    }

    /**
     * 错误响应
     */
    public static <T> ResponseDto<T> error(String msg) {
        return new ResponseDto<>(null, msg, 500);
    }

    /**
     * 错误响应（自定义状态码）
     */
    public static <T> ResponseDto<T> error(String msg, int code) {
        return new ResponseDto<>(null, msg, code);
    }

    /**
     * 未授权响应
     */
    public static <T> ResponseDto<T> unauthorized(String msg) {
        return new ResponseDto<>(null, msg, 401);
    }

    /**
     * 禁止访问响应
     */
    public static <T> ResponseDto<T> forbidden(String msg) {
        return new ResponseDto<>(null, msg, 403);
    }

    /**
     * 未找到响应
     */
    public static <T> ResponseDto<T> notFound(String msg) {
        return new ResponseDto<>(null, msg, 404);
    }

    public T getData() { return data; }
    public void setData(T data) { this.data = data; }
    public String getMsg() { return msg; }
    public void setMsg(String msg) { this.msg = msg; }
    public int getCode() { return code; }
    public void setCode(int code) { this.code = code; }


} 